package com.example.designpatterns.iterator;

import com.example.designpatterns.iterator.advanced.TreeCollection;
import com.example.designpatterns.iterator.concrete.Book;
import com.example.designpatterns.iterator.concrete.BookCollection;

/**
 * 迭代器模式演示类
 */
public class IteratorPatternDemo {

    public static void main(String[] args) {
        System.out.println("迭代器模式演示");
        System.out.println("=================");
        
        // 1. 图书集合迭代器演示
        System.out.println("\n1. 图书集合迭代器示例：");
        demonstrateBookIterator();
        
        // 2. 二叉树迭代器演示
        System.out.println("\n2. 二叉树遍历迭代器示例：");
        demonstrateTreeIterator();
    }
    
    private static void demonstrateBookIterator() {
        // 创建图书集合
        BookCollection bookCollection = new BookCollection(10);
        
        // 添加图书
        bookCollection.addBook(new Book("Java编程思想", "Bruce Eckel", "编程", 108));
        bookCollection.addBook(new Book("设计模式", "GoF", "编程", 85));
        bookCollection.addBook(new Book("重构", "Martin Fowler", "编程", 69));
        bookCollection.addBook(new Book("算法导论", "Cormen", "算法", 128));
        bookCollection.addBook(new Book("数据结构", "严蔚敏", "算法", 58));
        bookCollection.addBook(new Book("哈利波特", "J.K.罗琳", "小说", 42));
        bookCollection.addBook(new Book("指环王", "托尔金", "小说", 75));
        
        // 使用标准迭代器遍历所有图书
        System.out.println("遍历所有图书：");
        Iterator<Book> iterator = bookCollection.createIterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            System.out.println(" - " + book.getName() + " (" + book.getCategory() + ")");
        }
        
        // 使用类别迭代器遍历编程类图书
        System.out.println("\n遍历编程类图书：");
        Iterator<Book> programmingIterator = bookCollection.createCategoryIterator("编程");
        while (programmingIterator.hasNext()) {
            Book book = programmingIterator.next();
            System.out.println(" - " + book.getName() + " (" + book.getAuthor() + ")");
        }
        
        // 使用类别迭代器遍历小说类图书
        System.out.println("\n遍历小说类图书：");
        Iterator<Book> novelIterator = bookCollection.createCategoryIterator("小说");
        while (novelIterator.hasNext()) {
            Book book = novelIterator.next();
            System.out.println(" - " + book.getName() + " (" + book.getAuthor() + ")");
        }
    }
    
    private static void demonstrateTreeIterator() {
        // 创建二叉树集合
        TreeCollection<Integer> treeCollection = new TreeCollection<>();
        
        // 添加元素，构建二叉搜索树
        treeCollection.add(50);
        treeCollection.add(30);
        treeCollection.add(70);
        treeCollection.add(20);
        treeCollection.add(40);
        treeCollection.add(60);
        treeCollection.add(80);
        
        // 用中序遍历迭代器
        System.out.println("中序遍历 (左->根->右)：");
        Iterator<Integer> inOrderIterator = treeCollection.createIterator();
        printTreeTraversal(inOrderIterator);
        
        // 前序遍历
        System.out.println("\n前序遍历 (根->左->右)：");
        Iterator<Integer> preOrderIterator = treeCollection.createPreOrderIterator();
        printTreeTraversal(preOrderIterator);
        
        // 后序遍历
        System.out.println("\n后序遍历 (左->右->根)：");
        Iterator<Integer> postOrderIterator = treeCollection.createPostOrderIterator();
        printTreeTraversal(postOrderIterator);
    }
    
    private static <T> void printTreeTraversal(Iterator<T> iterator) {
        StringBuilder result = new StringBuilder();
        while (iterator.hasNext()) {
            result.append(iterator.next()).append(" ");
        }
        System.out.println(result.toString().trim());
    }
} 